package com.core.ui.customtag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.core.protocols.IPageHeaderDatasource;
import com.core.protocols.IStackNavigatorDelegate;
import com.core.utils.HTMLTagUtils;
import com.core.utils.StackItem;
import com.model.KeyValue;

public class PageHeader
        extends TagSupport
        implements IStackNavigatorDelegate {

	private static final long     serialVersionUID = 1L;
	private String                caption;
	private String                avatar;
	private IPageHeaderDatasource datasource       = null;



	public String getCaption () {

		return caption;
	}



	public void setCaption ( String caption ) {

		this.caption = caption;
	}



	public String getAvatar () {

		return avatar;
	}



	public void setAvatar ( String avatar ) {

		this.avatar = avatar;
	}



	public IPageHeaderDatasource getDatasource () {

		return datasource;
	}



	public void setDatasource ( IPageHeaderDatasource datasource ) {

		this.datasource = datasource;
	}



	@Override
	public void setPageCaption ( String value ) {

		this.caption = value;

	}



	@Override
	public void setPageAvatar ( String value ) {

		this.avatar = value;

	}



	public int doStartTag ()
	        throws JspException {

		String stackItems = null;
		if ( datasource != null ) {

			datasource.reload();


			this.caption = datasource.pageCaption();
			this.avatar = datasource.pageAvatar();
			List <StackItem> list = datasource.itemsList();

			boolean needSeparator = false;

			StringBuilder stackBuilder = new StringBuilder();

			for ( ListIterator <StackItem> iterator = list.listIterator(list.size()); iterator.hasPrevious(); ) {
				final StackItem stackItem = iterator.previous();

				if ( needSeparator ) {
					stackBuilder.append("  /  ");
				}

				List <KeyValue> attrs = new ArrayList <KeyValue>();
				attrs.add(new KeyValue("class", "trigger_lnk", true));
				attrs.add(new KeyValue("href", stackItem.getUrl(), true));

				String link = HTMLTagUtils.customTag("a", attrs, stackItem.getText());

				stackBuilder.append(link);

				needSeparator = true;
			}

			stackItems = stackBuilder.toString();
		}

		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"css/headline.css\">");
		stringBuilder.append("<div class=\"item-header\">");

		String avatar = HTMLTagUtils.image(this.avatar, "avatar", "");
		stringBuilder.append(avatar);
		stringBuilder.append("<div class=\"stack\">");
		if ( stackItems != null ) {
			stringBuilder.append(stackItems);
		}
		stringBuilder.append("</div>");

		String headline = HTMLTagUtils.h3("headline", this.caption);
		stringBuilder.append(headline);
		stringBuilder.append("</div>");
		try {
			JspWriter writer = pageContext.getOut();
			writer.append(stringBuilder.toString());
		} catch ( IOException e ) {
			throw new JspException(e.getMessage());
		}

		return SKIP_BODY;
	}
}
